<template>
  <div>

    <div class="card" style="margin-bottom: 10px;">
      <el-input prefix-icon="Search" style="width: 300px; margin-right: 10px" placeholder="请输入餐品名称查询" v-model="data.name"></el-input>
      <el-button type="primary" @click="load">查询</el-button>
      <el-button type="info" style="margin: 0 10px" @click="reset">重置</el-button>
    </div>

    <div class="card" style="margin-bottom: 10px">
      <div style="margin-bottom: 10px">
        <el-button type="primary" @click="handleAdd" >新增</el-button>
      </div>
      <el-table :data="data.tableData">
        <el-table-column prop="id" label="序号" width="70"/>
        <el-table-column prop="name" label="名称"/>
        <el-table-column prop="descr" label="简介"/>
        <el-table-column prop="price" label="价格"></el-table-column>
        <el-table-column label="图片">
         <template v-slot="scope">
           <el-image style="width: 100px; height: 100px; display: block" v-if="scope.row.img" :src="scope.row.img" :preview-src-list="[scope.row.img]" preview-teleported></el-image>
         </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button type="primary" @click="handleEdit(scope.row)" >编辑</el-button>
            <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="card" v-if="data.total">
      <el-pagination background layout="prev, pager, next" @current-change="load" :page-size="data.pageSize" v-model:current-page="data.pageNum" :total="data.total"/>
    </div>
    <el-dialog v-model="data.formVisible" title="信息" width="40%" destory-on-close>
      <el-form :model="data.form" label-width="100px" style="padding-right: 50px">
        <el-form-item label="名称" >
          <el-input v-model="data.form.name" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="简介" >
          <el-input  type="textarea" v-model="data.form.descr" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="价格" >
          <el-input v-model="data.form.price" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="图片">
          <el-upload action="http://localhost:8080/ordering/files/upload" :on-success="handleFileUpload">
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="data.formVisible = false">取消</el-button>
          <el-button type="primary" @click="save">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {reactive} from "vue"
import request from "../../utils/request";
import {ElMessage, ElMessageBox} from "element-plus";

const data = reactive({
  tableData: [],
  total: 0,
  pageNum: 1,  // 当前的页码
  pageSize: 5,  // 每页的个数
  formVisible: false,
  form: {},
  name: '',
})

const load = () =>{
  request.post('/foods/selectFoodListPage', {
      pageNum:data.pageNum,
      pageSize:data.pageSize,
      name:data.name
  }).then(res => {
    data.tableData = res.data?.list || []
    data.total = res.data.total
  })
}

load()
const reset = () =>{
  data.name = null
  load()
}
const handleAdd = () =>{
  data.form = {}//初始化表单
  data.formVisible = true //打开弹窗
}
//保存数据
const save = () =>{
   request.request({
     method: data.form.id ? 'PUT' : 'POST',
     url: data.form.id ? 'foods/updateFood' : 'foods/addFood',
     data:data.form
   }).then(res =>{
     if (res.code ===200){
       ElMessage.success('操作成功')
       data.formVisible = false//关闭弹窗
       load()//重新加载表格数据
     }else {
       ElMessage.error(res.msg)
     }

   })
}
const handleEdit = (row) =>{
  data.form = JSON.parse(JSON.stringify(row))
  data.formVisible = true
}
const del = (id) => {
  ElMessageBox.confirm('删除后数据无法恢复，您确认删除吗？', '确认删除', { type: 'warning' }).then(res => {
    request.get('/foods/deleteFoodById'+ '?id=' + id).then(res => {
      if (res.code === 200) {  //成功
        ElMessage.success('操作成功')
        load()  // 重新加载表格数据
      } else {
        ElMessage.error(res.msg)
      }
    })
  }).catch(err => {
    console.log(err)
  })
}

const handleFileUpload = (file) => {
  data.form.img = file.data
}

</script>
